package com.hzncc.flowable_diboot.service.impl;

import com.diboot.core.service.impl.BaseServiceImpl;
import com.diboot.core.vo.JsonResult;
import com.diboot.iam.service.SystemConfigService;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.coderule.service.CodeRuleService;
import com.hzncc.flowable_diboot.entity.Customer;
import com.hzncc.flowable_diboot.entity.CustomerFollowRecord;
import com.hzncc.flowable_diboot.entity.ToDoList;
import com.hzncc.flowable_diboot.mapper.CustomerFollowRecordMapper;
import com.hzncc.flowable_diboot.service.CustomerFollowRecordService;
import com.hzncc.flowable_diboot.service.CustomerService;
import com.hzncc.flowable_diboot.service.ToDoListService;
import com.hzncc.flowable_diboot.util.ConstantUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Objects;


/**
 * 客户跟进记录 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-21
 * Copyright © MyCorp
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CustomerFollowRecordServiceImpl extends BaseServiceImpl<CustomerFollowRecordMapper, CustomerFollowRecord> implements CustomerFollowRecordService {
    private final CustomerService customerService;
    private final SystemConfigService systemConfigService;
    private final ToDoListService toDoListService;
    private final CodeRuleService codeRuleService;
    ;

    @Override
    public JsonResult<?> follow(CustomerFollowRecord record) {
        if (record.getOtherActive()) {
            record.setCode(codeRuleService.generateCode(ConstantUtil.CODE_RULE_BUS_FOLLOW));
        }
        LocalDateTime nextConnectTime = record.getNextConnectTime();
        if (nextConnectTime != null && nextConnectTime.isBefore(LocalDateTime.now())) {
            return JsonResult.FAIL_INVALID_PARAM("下次跟进时间不能早于当前时间");
        }
        // 跟进记录
        record.setUserId(IamSecurityUtils.getCurrentUserId()); //跟进人
        //record.setCode(codeRuleService.generateCode(ConstantUtil.CODE_RULE_CUSTOMER_FOLLOW));
        Customer entity = customerService.getEntity(record.getCustomerId());
        // 更新客户跟进时间和联系时间
        if (entity != null) {
            entity.setLastFollwUpTime(LocalDateTime.now());
            entity.setNextConnectTime(nextConnectTime);
            // 重置过期时间
            BigDecimal enterThreshold = systemConfigService.findConfigValue(ConstantUtil.VALID, ConstantUtil.VALID_CUSTOMER);
            entity.setExpirationTime(enterThreshold.toString());
            // 生成代办
            if (nextConnectTime != null) {
                toDoListService.createToDo(new ToDoList()
                        .setModel(ConstantUtil.MODEL_CUSTOMER) // 业务模块
                        .setMission(String.format(ConstantUtil.CUSTOMER_FOLLOW_MSG_TEMPLATE, entity.getName())) // 待办事项
                        .setStartTime(nextConnectTime) // 待办开始时间
                        .setExecutor(record.getUserId()) // 待办执行人
                        .setCustomer(entity.getId()) // 客户
                        .setContactWay(record.getContactWay()) // 联系方式
                        .setType(ConstantUtil.TODAY_CUSTOMER) // 类别
                );
            }
            customerService.updateEntity(entity);
        }
        this.createEntity(record);
        return JsonResult.OK();
    }

    /**
     * 编辑跟进记录
     *
     * @param record
     * @param id
     * @return
     * @author 陈广煜
     * @date 2024-08-20
     */
    public JsonResult<?> followEdit(CustomerFollowRecord record, String id) {
        if (!record.getUserId().equals(IamSecurityUtils.getCurrentUserId())) {
            return JsonResult.FAIL_INVALID_PARAM("只能编辑自己的跟进记录");
        }
        record.setId(id);
        CustomerFollowRecord entity = getEntity(id);
        Customer customer = customerService.getEntity(record.getCustomerId());
        // 下次跟进事件变化
        if (!Objects.equals(entity.getNextConnectTime(), record.getNextConnectTime()) && record.getNextConnectTime() != null && entity.getNextConnectTime() != null) {
            LocalDateTime nextConnectTime = record.getNextConnectTime();
            if (nextConnectTime.isBefore(LocalDateTime.now())) {
                return JsonResult.FAIL_INVALID_PARAM("下次跟进时间不能早于当前时间");
            }
            toDoListService.createToDo(new ToDoList()
                    .setModel(ConstantUtil.MODEL_CUSTOMER) // 业务模块
                    .setMission(String.format(ConstantUtil.CUSTOMER_FOLLOW_MSG_TEMPLATE, customer.getName())) // 待办事项
                    .setStartTime(nextConnectTime) // 待办开始时间
                    .setExecutor(record.getUserId()) // 待办执行人
                    .setCustomer(entity.getId()) // 客户
                    .setContactWay(record.getContactWay()) // 联系方式
                    .setType(ConstantUtil.TODAY_CUSTOMER) // 类别
            );
        }
        updateEntity(record);
        return JsonResult.OK();
    }
}
